//
//  NSDate+Additions.m
//  ZYFramework
//
//  Created by 赵燕 on 2019/6/14.
//  Copyright © 2019 赵燕. All rights reserved.
//

#import "NSDate+Additions.h"

@implementation NSDate (Additions)

-(NSString *)stringFromFormat:(NSString *)dateFormat{
    
    return [NSDate stringFromDate:self format:dateFormat timeZone:nil];
}

-(NSString *)stringFromFormat:(NSString *)dateFormat timeZone:(NSTimeZone *)timeZone{
    
    return [NSDate stringFromDate:self format:dateFormat timeZone:timeZone];
}

+(NSString *)timestampFromDate:(NSDate *)date msAccurated:(BOOL)yesOrNo{
    
    if ([date isKindOfClass:NSDate.class]) {
        NSInteger times =yesOrNo?1000:1;
        NSTimeInterval timeInterval =[date timeIntervalSince1970]*times;
        return [NSString stringWithFormat:@"%.0lf",timeInterval];
    }
    return @"";
}

+(NSDate *)dateFromUnixString:(NSString *)unixString msAccurated:(BOOL)yesOrNo{
    
    NSTimeInterval interval =0;
    
    if ([unixString isKindOfClass:NSString.class]||[unixString isKindOfClass:NSNumber.class]||[unixString isKindOfClass:NSDecimalNumber.class]) {
        interval =[unixString doubleValue];
    }
    NSInteger times =yesOrNo?1000:1;
    interval =interval/times;
    return [NSDate dateWithTimeIntervalSince1970:interval];
}

+(NSString *)stringFromDate:(NSDate *)date format:(NSString *)dateFormat{
    
    return [NSDate stringFromDate:date format:dateFormat timeZone:nil];
}

+(NSString *)stringFromDate:(NSDate *)date format:(NSString *)dateFormat timeZone:(NSTimeZone *)timeZone{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    if (timeZone)
        [formatter setTimeZone:timeZone];
    return [formatter stringFromDate:date];
}

+(NSDate *)dateFromString:(NSString *)string format:(NSString *)dateFormat{
    
    return [NSDate dateFromString:string format:dateFormat timeZone:nil];
}

+(NSDate *)dateFromString:(NSString *)string format:(NSString *)dateFormat timeZone:(NSTimeZone *)timeZone{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:dateFormat];
    if (timeZone)
        [formatter setTimeZone:timeZone];
    return [formatter dateFromString:string];
}

+ (NSUInteger)yearFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitYear) fromDate:date];
    return dayComponents.year;
}

+ (NSUInteger)monthFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMonth) fromDate:date];
    return dayComponents.month;
}

+ (NSUInteger)dayFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitDay) fromDate:date];
   
    return dayComponents.day;
}

+ (NSUInteger)hourFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitHour) fromDate:date];
    return dayComponents.hour;
}

+ (NSUInteger)minuteFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitMinute) fromDate:date];
    return dayComponents.minute;
}

+ (NSUInteger)secondFromDate:(NSDate *)date{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dayComponents = [calendar components:(NSCalendarUnitSecond) fromDate:date];
    return dayComponents.second;
}

+ (BOOL)isLeapYear:(NSDate *)date{
    
    NSUInteger year = [NSDate yearFromDate:date];
    if ((year % 4  == 0 && year % 100 != 0) || year % 400 == 0) {
        return YES;
    }
    return NO;
}

+ (NSUInteger)daysInYearFromDate:(NSDate *)date{
    
    return [NSDate isLeapYear:date]?366:365;
}

+ (NSUInteger)daysInMonthFromDate:(NSDate *)date{
    
    NSUInteger monthIndex =[NSDate monthFromDate:date];
    switch (monthIndex) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 2:
            return [NSDate isLeapYear:date]?29:28;
    }
    return 30;
}


+(NSUInteger)weekdayFromDate:(NSDate *)date{
    
    NSCalendar *calendar =[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitWeekday) fromDate:date];
    return components.weekday;
}

+ (BOOL)isSameYear:(NSDate *)date1 compare:(NSDate *)date2{
    
    BOOL sameYear =[NSDate yearFromDate:date1]==[NSDate yearFromDate:date2];
    return sameYear;
}

+ (BOOL)isSameMonth:(NSDate *)date1 compare:(NSDate *)date2{
    
    BOOL sameYear =[NSDate yearFromDate:date1]==[NSDate yearFromDate:date2];
    BOOL sameMonth =[NSDate monthFromDate:date1]==[NSDate monthFromDate:date2];
    return sameYear&&sameMonth;
}

+ (BOOL)isSameDay:(NSDate *)date1 compare:(NSDate *)date2{
    
    BOOL sameYear =[NSDate yearFromDate:date1]==[NSDate yearFromDate:date2];
    BOOL sameMonth =[NSDate monthFromDate:date1]==[NSDate monthFromDate:date2];
    BOOL sameDay =[NSDate dayFromDate:date1]==[NSDate dayFromDate:date2];
    return sameYear&&sameMonth&&sameDay;
}



@end
